const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
          linkCode: options.linkCode
        })
      }
    })
    wx.showLoading({
      title: '加载中',
    })
    this.authorization();
    
  },
  onShow: function () {

  },
  shareSub:function(){
    var params = new Object()
    var linkCode = this.data.linkCode;
    var that = this;
    params.linkCode = linkCode;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'couponsbag/shareStatus',
      success: function (res) {
        wx.hideLoading()
        if (util.isEmpty(res.data.code)) {
          // util.showStatus(res.data.message);
          console.log(res.data.data);
          that.setData({
            shareDetail:res.data.data
          })

        }
      },
      fail: function (fail) {
        wx.hideLoading()
        util.showError("登录失败，请检查网络");

      }
    })
  },
  shareReceive:function(){
    var params = new Object()
    var linkCode = this.data.linkCode;
    var that = this;
    params.linkCode = linkCode;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'couponsbag/shareSub',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          util.showStatus(res.data.message);
          that.shareSub();
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
  // 授权
  authorization: function () {
    var that = this
    wx.login({
      success: function (res_login) {
        wx.getUserInfo({
          withCredentials: true,
          success: function (res_user) {
            var login_code = res_login.code;
            var login_encryptedData = res_user.encryptedData;
            var login_iv = res_user.iv;
            that.autoLogin(login_code, login_encryptedData, login_iv);
          }, fail: function (fail_user) {
            that.showModal();
          }
        })
      }, fail: function (fail_login) {
        console.log("fail");
      }
    })
  },

  // 重新授权
  reAuthorization: function () {
    var that = this
    wx.openSetting({
      success: function (res) {
        if (res.authSetting["scope.userInfo"]) {
          that.authorization();
        }
      }
    })
  },

  //未登录警告框
  showModal: function () {
    var that = this
    wx.showModal({
      title: '警告',
      content: '若不授权登录，则无法使用',
      confirmText: '授权',
      cancelText: '不授权',
      success: function (suress) {
        if (suress.confirm) {
          that.reAuthorization();
        } else {
          that.showModal();
        }
      }
    })
  },

  //登录请求
  autoLogin: function (login_code, login_encryptedData, login_iv) {
    var that = this;
    var params = new Object()
    params.code = login_code;
    params.encryptedData = login_encryptedData;
    params.iv = login_iv;

    api.POST({
      params: params,
      url: app.globalData.base_url + 'user/login',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          app.globalData.accountInfo = res.data.data.result;
        }
        that.shareSub();
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  }
})